﻿using System.Windows;
using LivePushFlow.Views;
using System.Windows.Input;

namespace LivePushFlow
{
    public partial class MainWindow : Window
    {
        // 记录鼠标相对于窗口的位置
        private Point _offset; 

        public MainWindow()
        {
            InitializeComponent();
            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            // 添加鼠标事件处理程序
            this.PreviewMouseLeftButtonDown += OnLeftMouseDown;
            this.PreviewMouseMove += OnMouseMoveOrUp;

            // 模拟检查程序更新
            var timer = new System.Timers.Timer(2000);
            timer.AutoReset = false;
            timer.Elapsed += (source, e) =>
            {
                Dispatcher.Invoke(new Action(() => 
                {
                    var pushFlow = new PushFlow();
                    pushFlow.Show();
                    this.Close();
                }));
                timer.Close();
                timer.Dispose();
            };
            timer.Start();
        }

        private void OnLeftMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left && e.ClickCount == 1)
            {
                _offset = e.GetPosition(this);
                _offset = new Point(_offset.X - (this.Left), _offset.Y - (this.Top));
                DragMove(); // 开始拖动窗口
            }
        }

        private void OnMouseMoveOrUp(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Released)
            {
                _offset = new Point();
            }
        }

    }
}